---@diagnostic disable: undefined-global

--- 智识技能
--- 出牌阶段限两次，你可以交给其他角色一张牌。若其手牌花色数因此增加，你与其各获得一张【梨】，否则你观看其手牌并摸一张牌。
local zhishi = fk.CreateSkill{
  name = "lvshi__zhishi",
}

-- 获取手牌花色数
local function getSuitCount(player)
  local suits = {}
  for _, card_id in ipairs(player:getCardIds("h")) do
    local card = Fk:getCardById(card_id)
    if card then
      suits[card.suit] = true
    end
  end
  local count = 0
  for _ in pairs(suits) do
    count = count + 1
  end
  return count
end

-- 创建主动技能
zhishi:addEffect("active", {
  anim_type = "support",
  prompt = "#ls__zhishi-active",
  card_num = 1,
  target_num = 1,
  max_phase_use_time = 2,
  card_filter = Util.TrueFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    -- 记录原始花色数
    local original_suit_count = getSuitCount(target)
    
    -- 交给牌
    room:moveCardTo(effect.cards[1], Card.PlayerHand, target, fk.ReasonGive, zhishi.name, nil, false, player.id)
    
    -- 判断花色数是否增加
    local new_suit_count = getSuitCount(target)
    
    if new_suit_count > original_suit_count then
      -- 花色数增加，双方各获得一张【梨】
      local pear_card = room:printCard("pear", Card.NoSuit, 0)
      room:obtainCard(player, pear_card, true, fk.ReasonJustMove, player, zhishi.name)
      room:setCardMark(pear_card, "zhishi__destr_discard")
      local pear_card_to = room:printCard("pear", Card.NoSuit, 0)
      room:obtainCard(target, pear_card_to, true, fk.ReasonJustMove, target, zhishi.name)
      room:setCardMark(pear_card_to, "zhishi__destr_discard")
    else
      -- 花色数未增加，观看手牌并摸一张牌
      room:viewCards(player, {
        cards = target:getCardIds("h"),
        skill_name = zhishi.name,
        prompt = "$ViewCardsFrom:" .. target.id
      })
      player:drawCards(1, zhishi.name)
    end
  end,
})

Fk:loadTranslationTable{
  ["lvshi__zhishi"] = "智识",
  [":lvshi__zhishi"] = "出牌阶段限两次，你可以交给其他角色一张牌。若其手牌花色数因此增加，你与其各获得一张【梨】，否则你观看其手牌并摸一张牌。",
  ["$lvshi__zhishi1"] = "知己知彼，百战不殆。",
  ["$lvshi__zhishi2"] = "识人善任，方能制胜。",
  
  ["#ls__zhishi-active"] = "智识：你可以交给其他角色一张牌",
}

return zhishi